package com.holiday.springboot2.controller;


import com.holiday.springboot2.domain.User;
import com.holiday.springboot2.service.UserService;
import com.holiday.springboot2.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1/pub/user")
public class UserController {

    /**
     * String pwd, String username ：这种方式不能接收json格式的请求
     * @param pwd
     * @param username
     * @return
     */
    @PostMapping("login")
    public JsonData login(String pwd, String username) {
        System.out.println(pwd+"--"+username);
        return JsonData.buildSuccess(pwd + username);
    }

    /**
     *  使用对象接收参数值，参数多的话，可以使用对象，参数名称需要与对象一致
     * @param user
     * @return
     */
    @PostMapping("login1")
    public JsonData login1(User user) {
        System.out.println(user.getUserName()+"--"+user.getPwd());
        return JsonData.buildSuccess(user.getUserName() + user.getPwd());
    }


    @Autowired
    private UserService userService;

    /**
     * 以json格式提交的参数请求接口 @RequestBody  User user
     * @param user
     * @return
     */
    @PostMapping("login2")
    public JsonData login2(@RequestBody  User user) {
        String token = userService.login(user.getUserName(), user.getPwd());
        return token!=null?JsonData.buildSuccess(token):JsonData.buildError("账号或密码错误");
    }





}
